$(function () {
    /*员式数据列表*/
    $("#dg").datagrid({
        url:"/employeeList",
        columns:[[
            {field:'username',title:'姓名',width:100,align:'center'},
            {field:'inputtime',title:'入职时间',width:100,align:'center'},
            {field:'tel',title:'电话',width:100,align:'center'},
            {field:'email',title:'邮箱',width:100,align:'center'},
            {field:'department',title:'部门',width:100,align:'center',formatter: function(value,row,index){
                 if (value){

                        return value.name;
                 }
                }},
            {field:'state',title:'状态',width:100,align:'center',formatter: function(value,row,index){

                if (row.state){
                    return "在职";
                } else {

                    return "<font style='color: red'>离职</font>"
                }

                }},
            {field:'admin',title:'管理员',width:100,align:'center',formatter: function(value,row,index){

                    if (row.admin){
                        return "是";
                    } else {
                        return "<font style='color: red'>否</font>"
                    }

                }}
        ]],
        fit:true,
        fitColumns:true,
        rownumbers:true,
        pagination:true,
        singleSelect:true,
        toolbar: '#tb',
        onClickRow:function (rowIndex, rowData) {
            if (!rowData.state){
                $("#delete").linkbutton("disable");
            } else {
                $("#delete").linkbutton("enable");
            }
        }

    });

    /*点击添加*/
    $("#add").click(function () {
        /*取消密码验证*/
        $("[name='password']").validatebox({required:true});
        $("#password").show();
        $("#dialog").dialog("setTitle","添加员工");
        $("#employeeForm").form("clear");
        $("#dialog").dialog("open");
    });

    /*点击编辑*/
    $("#edit").click(function () {
        /*获取当前选中的行*/
        var rowData = $("#dg").datagrid("getSelected");
        if(!rowData){
            $.messager.alert("提示","选择一行数据进行编辑");
            return;
        }

        /*取消密码验证*/
        $("[name='password']").validatebox({required:false});
        $("#password").hide();
        $("#dialog").dialog("setTitle","编辑员工");
        $("#employeeForm").form("clear");
        $("#dialog").dialog("open");
        if (rowData["department"]){
            rowData["department.id"] = rowData["department"].id;
        }

        if(rowData["admin"] != null) {
            rowData["admin"] = rowData["admin"] + "";
        }
        /*回显角色*/
        /*根据当前用户的id,查出对应的角色*/
        $.get("/getRoleByEid?id="+rowData.id,function (data) {
            /*设置下拉列表数据回显*/
            $("#role").combobox("setValues",data);
        });
        console.log(rowData);
        //回显数据
        $("#employeeForm").form("load",rowData);
    });


    // 点击删除
    $("#delete").click(function () {
        /*获取当前选中的行*/
        var rowData = $("#dg").datagrid("getSelected");
        console.log(rowData);
        if (!rowData) {
            $.messager.alert("提示", "选择一行数据进行删除");
            return;
        }
        /*弹出提示框*/
        $.messager.confirm("确认","您确定要设置为离职",function (res) {
            if (res){
            //修改员工状态
                $.get("/employeeUpdateState?id="+rowData.id,function (data) {
                    if (data.success){
                        $.messager.alert("温馨提示",data.message,"info",function () {
                            $("#dg").datagrid("reload");
                        })
                    }
                })
            }
        })
    });

    //点击搜索
    $("#searchbtn").click(function () {

        //获取输入内容
        var keyword = $("[name='keyword']").val();
        //重新加载数据
        $("#dg").datagrid("load",{keyword:keyword});
    });

    // 点击刷新
    $("#reload").click(function () {
        var keyword = $("[name='keyword']").val("");
        $("#dg").datagrid("load",{});
    });

    /*弹出框*/
    $("#dialog").dialog({
        width:370,
        height:370,
        closed:true,
        buttons:[{
        text:'保存',
        handler:function(){

            /*获取当前选中的行*/
            var id = $("[name='id']").val();
            var url;
            if(id){
               url = "/updateEmployee";
            }else {
                url = "/saveEmployee";
            }
            $("#employeeForm").form("submit",{
               url:url,
                onSubmit:function(param){
                // 获取选中的角色
                    var values = $("#role").combobox("getValues");
                    for (i = 0; i < values.length; i++){
                        param["roleList["+i+"].rid"] = values[i];
                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示",data.message);
                        /*关闭对话框 */
                        $("#dialog").dialog("close");
                        /*重新加载数据表格*/
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("温馨提示",data.message);
                    }
                }
            });
        }
    },{
        text:'关闭',
        handler:function(){
            /*关闭对话框 */
            $("#dialog").dialog("close");
        }
    }]
    });

    /*部门选择 下拉列表*/
    $("#department").combobox({
        width:150,
        panelHeight:'auto',
        editable:false,
        url:'departList',
        textField:'name',
        valueField:'id',
        onLoadSuccess:function () { /*数据加载完毕之后回调*/
            $("#department").each(function(i){
                var span = $(this).siblings("span")[i];
                var targetInput = $(span).find("input:first");
                if(targetInput){
                    $(targetInput).attr("placeholder", $(this).attr("placeholder"));
                }
            });
        }
    });

    /*管理员 下拉列表*/
    $("#isAdmin").combobox({
        width:150,
        panelHeight:'auto',
        editable:false,
        textField:'label',
        valueField:'value',
        data:[{
            label:'是',
            value:'true'
        },{
            label:'否',
            value:'false'
        }],
        onLoadSuccess:function () { /*数据加载完毕之后回调*/
            $("#isAdmin").each(function(i){
                var span = $(this).siblings("span")[i];
                var targetInput = $(span).find("input:first");
                if(targetInput){
                    $(targetInput).attr("placeholder", $(this).attr("placeholder"));
                }
            });
        }
    });

    /*父菜单选择 下拉列表*/
    $("#role").combobox({
        width:150,
        panelHeight:'auto',
        editable:false,
        url:'selectRoles',
        textField:'rname',
        valueField:'rid',
        multiple:true,
        onLoadSuccess:function (data) {
            /*数据加载完毕之后回调*/
            $("#role").each(function(i){
                var span = $(this).siblings("span")[i];
                var targetInput = $(span).find("input:first");
                if(targetInput){
                    $(targetInput).attr("placeholder", $(this).attr("placeholder"));
                }
            });
        }
    });

    $("#excelUpload").dialog({
        width:260,
        height:180,
        title:"导入Excel",
        buttons:[{
            text:'保存',
            handler:function(){
                $("#excelUploadForm").form("submit",{
                    url:"/uploadExcelFile",
                    success:function (data) {
                        data = $.parseJSON(data);
                        if (data.success){
                            $.messager.alert("温馨提示",data.message);
                            /*关闭对话框 */
                            $("#excelUpload").dialog("close");
                            /*重新加载数据表格*/
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("温馨提示",data.message);
                        }
                    }
                });
            }
        },{
            text:'关闭',
            handler:function(){
                $("#excelUpload").dialog("close");
            }
        }],
        closed:true
    });

    $("#excelImpot").click(function () {
        $("#excelUpload").dialog("open");
    });

    // 点击导出
    $("#export").click(function () {
        window.open("/downExcel");
    });


    // 点击导入
    $("#import").click(function () {
        $("#excelUpload").dialog("open");
    });

    // 下载Excel模板
    $("#downloadTml").click(function () {
        window.open('/downloadExcelTpl')
    });

});